﻿using System.Text;

namespace Utils
{
    public static class CommoHelper
    {
        #region 文件操作

        public static FileInfo[] GetFiles(string directoryPath)
        {
            if (!IsExistDirectory(directoryPath))
            {
                throw new DirectoryNotFoundException($"目录未找到: {directoryPath}");
            }
            var root = new DirectoryInfo(directoryPath);
            return root.GetFiles();
        }

        public static bool IsExistDirectory(string directoryPath)
        {
            return Directory.Exists(directoryPath);
        }

        public static string ReadFile(string path)
        {
            if (!File.Exists(path))
            {
                return "不存在相应的文件";
            }

            // 使用 using 语句确保 StreamReader 被正确释放
            using (var reader = new StreamReader(path, Encoding.Default))
            {
                return reader.ReadToEnd();
            }
        }

        public static void FileMove(string originFile, string newFile)
        {
            if (File.Exists(originFile))
            {
                File.Move(originFile, newFile);
            }
            else
            {
                throw new FileNotFoundException($"源文件未找到: {originFile}");
            }
        }

        public static void CreateDir(string dir)
        {
            if (string.IsNullOrWhiteSpace(dir))
            {
                throw new ArgumentException("目录名称不能为空", nameof(dir));
            }

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
        }

        #endregion 文件操作

        public static string GenerateRandomString(int length)
        {
            var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            var stringChars = new char[length];
            var random = new Random();

            for (int i = 0; i < stringChars.Length; i++)
            {
                stringChars[i] = chars[random.Next(chars.Length)];
            }

            return new String(stringChars);
        }

        private static Random random = new();
        private static string[] firstNames = { "赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许", "何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏", "陶", "姜", "戚", "谢", "邹", "喻", "柏", "水", "窦", "章", "云", "苏", "潘", "葛", "奚", "范", "彭", "郎", "韦" }; // 常见姓氏数组
        private static string[] lastNames = { "天", "明", "辉", "煌", "高", "雅", "清", "新", "自然", "和谐", "和", "美", "丽", "浪", "漫", "浪", "漫", "梦", "幻" }; // 常见的名字风格描述词数组
        private static string[] lastNameSuffix = { "华", "子", "轩", "宇", "航", "浩", "博", "涵", "雨", "婷", "琪", "雪", "怡", "宁", "婉", "柔", "悦", "媛", "媛", "欣", "欣", "妍" }; // 常见的名字后缀数组

        public static string GenerateChineseRandomNickName()
        {
            // 随机选择一个姓氏
            string firstName = firstNames[random.Next(firstNames.Length)];

            // 随机选择一个名字描述词和一个名字后缀
            string lastNameFirst = lastNames[random.Next(lastNames.Length)];
            string lastNameLast = lastNameSuffix[random.Next(lastNameSuffix.Length)];

            // 组合昵称
            string nickName = $"{firstName}{lastNameFirst}{lastNameLast}";

            // 可以添加一些随机的特殊字符或者数字，使得昵称更加个性化

            var d = random.Next(1, 100);

            nickName += d > 50 ? $"{d}" : lastNameSuffix[random.Next(lastNameSuffix.Length)];

            return nickName;
        }

        public static bool ContainsChinese(this string input)
        {
            if (string.IsNullOrEmpty(input))
                return false;

            return input.Any(c =>
                (c >= 0x4e00 && c <= 0x9fff) || // 汉字范围
                (c >= 0x3400 && c <= 0x4dbf) ||
                (c >= 0x20000 && c <= 0x2a6df) ||
                char.IsSurrogate(c)); // 包含半字符
        }
    }
}